package com.wzdigit.wms.raw.task;

import com.wzdigit.framework.exception.RestStatusException;
import com.wzdigit.wms.raw.constant.SyncTypeConstant;
import com.wzdigit.wms.raw.domain.dto.IfPoHeaderDto;
import com.wzdigit.wms.raw.exception.SyncException;
import com.wzdigit.wms.raw.exception.SystemException;
import com.wzdigit.wms.raw.service.mid.IfPoHeaderService;
import com.wzdigit.wms.raw.service.mid.SyncErrorDataService;
import lombok.extern.slf4j.Slf4j;
import net.dreamlu.mica.core.utils.CollectionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @ClassName IfPoToPoTask
 * @Description 接口中间表-易损件 to 生产领料，退料
 * @Author LHR-PC
 * @Date 2021/6/11
 * @Version
 **/
@ConditionalOnExpression("${schedule.common.switch}")
@Component
@Slf4j
public class IfPoToPoOrGdnTask {

    @Autowired
    private IfPoHeaderService ifPoHeaderService;

    @Autowired
    private SyncErrorDataService syncErrorDataService;

    final String syncType = SyncTypeConstant.IF_PO_TO_PO;

    @Scheduled(cron = "0 0/5 * * * ?")
    public synchronized void syncData() {
        try {
            log.info("if:Po => gdn or asn start...");
            long start = System.currentTimeMillis();
            List<IfPoHeaderDto> datas = ifPoHeaderService.selectSyncData();
            if (!CollectionUtil.isEmpty(datas)) {
                syncData(datas, false);
            }
            log.info("------if:Po =>gdn or  asn end:{}------", System.currentTimeMillis() - start);
        } catch (RestStatusException ex) {
            ex.printStackTrace();
        }
    }

    public   void syncData(List<IfPoHeaderDto> datas, boolean isSyncError) {
        for (IfPoHeaderDto data : datas) {
            if("2".equals(data.getZtype())){   //同步到领料出库GDN
                try {
                    ifPoHeaderService.syncDataGdn(data, isSyncError);
                } catch (Exception e) {
                    exceptionHander(data, e, "采购订单同步失败，单号{}", "采购订单同步失败：", "【采购订单同步失败】");
                }
            }else if("1".equals(data.getZtype())){//退料到本厂 同步到ASN

               /* try {
                    ifPoHeaderService.syncDataPo(data, isSyncError);
                } catch (Exception e) {
                    exceptionHander(data, e, "发货单同步失败，单号{}", "发货单同步失败：", "【发货单同步失败】");
                }
*/
            }else{
                exceptionHander(data, new SystemException("未知的单据类型"+data.getZtype()), "易损件领退料同步失败，单号{}", "易损件领退料同步失败：", "【易损件领退料同步失败】");
            }

        }
    }

    private void exceptionHander(IfPoHeaderDto data, Exception e, String s, String s2, String s3) {
            log.warn(s, data.getEbeln());
        log.warn(s2 + e.getMessage() == null ? e.toString() : e.getMessage());
        String message = "";
        if (SyncException.class.isInstance(e)) {
            SyncException ex = (SyncException) e;
            message = ex.getFailedReason();
            if (message.length() >= 1000) {
                message = message.substring(0, 500);
            }
            log.info(s3 + ex.getFailedReason());
            syncErrorDataService.saveSyncErrorData(ex.getSyncErrorType(), ex.getCode(), message);
        } else {
            e.printStackTrace();
            if (e.getMessage() == null) {
                message = e.getClass().getName();
            } else {
                message = e.getMessage();
            }
            if (message.length() >= 1000) {
                message = message.substring(0, 500);
            }
            syncErrorDataService.saveSyncErrorData(syncType, data.getEbeln(), message);
        }
        e.printStackTrace();
    }
}
